package fr.orleans.match;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import entities.Event;
import entities.Match;
import entities.Player;
import entities.PlayerEvent;
import enums.Flag;
import enums.MatchStatus;
import enums.PlayerEventType;
import fr.orleans.tools.JSFServices;
import fr.orleans.tools.ManagedBeans;

@ManagedBean
@ViewScoped
public class AdminEditMatch extends ManagedBeans{

	private Match match;
	private PlayerEvent eventPlayer;
	private List<SelectItem> listPlayer;
	private Player playerSelected;
	private List<SelectItem> listEventPlayer;
	private PlayerEventType typeSelected;
	private List<PlayerEvent> listPlayerEvent;

	public AdminEditMatch(){
		try{
			this.match = (Match) JSFServices.getParameterInRequestMap("match");
		}catch(Exception e){
			
		}
	}
	public Match getMatch() {
		if(this.match == null)
			this.match = (Match) JSFServices.getParameterInRequestMap("match");
		
		return match;
	}

	public void setMatch(Match match) {
		this.match = match;
	}

	public String startMatch(){
		this.getMatch().setStatus(MatchStatus.running);
		this.getService().getWebAction().editMatch(getMatch());
		return "";
	}

	public String stopMatch(){
		if(getMatch().getScoreHome() == getMatch().getScoreOutside()){
			FacesMessage myFacesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, 
					"Le match doit avoir un vainqueur ! Match nul impossible", 
					"Le match doit avoir un vainqueur ! Match nul impossible");
			FacesContext myFacesContext = FacesContext.getCurrentInstance();
			myFacesContext.addMessage(null, myFacesMessage);
		}
		else{
			this.getMatch().setStatus(MatchStatus.terminated);
			this.getService().getWebAction().editMatch(getMatch());
		}
		
		return "";
	}

	public boolean isOdd(int value){
		return (value % 2) == 1;
	}
	
	public List<SelectItem> getListPlayer() {
		if(this.listPlayer == null){
			List<Player> tmpList = new ArrayList<>(this.match.getHomeTeam().getListPlayers());
			tmpList.addAll(this.match.getOutsideTeam().getListPlayers());
			
			this.listPlayer = new ArrayList<SelectItem>();
			
			for(Player p : tmpList){
				this.listPlayer.add(new SelectItem(p, p.toString()));
			}
		}
		
		return listPlayer;
	}
	
	public void setListPlayer(List<SelectItem> listPlayer) {
		this.listPlayer = listPlayer;
	}
	
	public PlayerEvent getEventPlayer() {
		return eventPlayer;
	}
	
	public void setEventPlayer(PlayerEvent eventPlayer) {
		this.eventPlayer = eventPlayer;
	}
	
	public Player getPlayerSelected() {
		if(this.playerSelected == null)
			this.playerSelected = new Player();
		
		return playerSelected;
	}
	
	public void setPlayerSelected(Player playerSelected) {
		this.playerSelected = playerSelected;
	}
	
	public String addEventToMatch(){
		PlayerEvent playerevent = new PlayerEvent();
		playerevent.setPlayer(this.playerSelected);
		playerevent.setDateEvent(new Date());
		playerevent.setMatch(this.match);
		playerevent.setOneFlag(Flag.activated);
		playerevent.setTypeEventPlayer(this.typeSelected);
		
		if(this.typeSelected.equals(PlayerEventType.goal)){
			if(this.playerSelected == null)
				System.out.println("player null");
			if(this.playerSelected.getTeam() == null)
				System.out.println("team null");
			if(this.playerSelected.getTeam().equals(this.match.getHomeTeam())){
				this.match.setScoreHome(this.match.getScoreHome() + 1);
			}else{
				this.match.setScoreOutside(this.match.getScoreOutside() + 1);
			}
		}
		if(this.typeSelected.equals(PlayerEventType.csc)){
			if(this.playerSelected.getTeam().equals(this.match.getHomeTeam())){
				this.match.setScoreOutside(this.match.getScoreOutside() + 1);
			}else{
				this.match.setScoreHome(this.match.getScoreHome() + 1);
			}
		}
		
		if(this.match.getListEvent() == null){
			this.match.setListEvent(new HashSet<Event>());
		}
		
		this.match.getListEvent().add(playerevent);
		this.getService().getWebAction().editMatch(this.match);
				
		return "";
	}
	
	public List<SelectItem> getListEventPlayer() {
		if(this.listEventPlayer == null){
			this.listEventPlayer = new ArrayList<SelectItem>();
			
			for(PlayerEventType type : PlayerEventType.values()){
				if(type.equals(PlayerEventType.goal))
					this.listEventPlayer.add(new SelectItem(type, "But pour son camp"));
				if(type.equals(PlayerEventType.csc))
					this.listEventPlayer.add(new SelectItem(type, "But contre son camp"));
				if(type.equals(PlayerEventType.yellowcard))
					this.listEventPlayer.add(new SelectItem(type, "Carton jaune"));
				if(type.equals(PlayerEventType.redcard))
					this.listEventPlayer.add(new SelectItem(type, "Carton rouge"));
			}
		}
		
		return listEventPlayer;
	}
	
	public void setListEventPlayer(List<SelectItem> listEventPlayer) {
		this.listEventPlayer = listEventPlayer;
	}
	
	public PlayerEventType getTypeSelected() {
		return typeSelected;
	}
	
	public void setTypeSelected(PlayerEventType typeSelected) {
		this.typeSelected = typeSelected;
	}
	
	public List<PlayerEvent> getListPlayerEvent() {
		List<Event> listTmp = new ArrayList<>(this.match.getListEvent());
		
		this.listPlayerEvent = new ArrayList<PlayerEvent>();
		
		for(Event event : listTmp){
			if(event.getClass().equals(PlayerEvent.class)){
				PlayerEvent playerE = (PlayerEvent) event;
				this.listPlayerEvent.add(playerE);
			}
		}
		
		return listPlayerEvent;
	}
	
	public void setListPlayerEvent(List<PlayerEvent> listPlayerEvent) {
		this.listPlayerEvent = listPlayerEvent;
	}
}